package net.akkz.action.feature;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.beanFactory.BeanFactory;
import net.akkz.model.Fulture;
import net.akkz.model.Student;
import net.akkz.server.FultureServer;
import net.akkz.server.StudentServer;

@WebServlet(name="FultureDetail", urlPatterns="/student/fultureDetail.do")
public class FultureDetail extends HttpServlet
{
	private FultureServer fultureServer;
	private StudentServer studentServer;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		doPost(request, response);
	}
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
	{
		Student student = (Student)request.getSession().getAttribute("student");
		
		int fultureId = Integer.parseInt(request.getParameter("fultureId"));
		
		if(student == null)
		{
			request.setAttribute("state", 1002);
		}
		else if(fultureServer.checkJoinFulture(fultureId, student.getId()))
		{
			request.setAttribute("state", 1001);
		}
		else
		{
			request.setAttribute("state", 1000);
		}
		
		request.setAttribute("fulture", fultureServer.getFulture(fultureId));
		request.setAttribute("joinedStudent", fultureServer.findFultureJoinedNameByFultureId(fultureId));
		
		request.getRequestDispatcher("/jsp/fulture.jsp").forward(request, response);
	}
	
	@Override
	public void init()
	{
		BeanFactory  bf = (BeanFactory) this.getServletContext().getAttribute("beanFactory");
		
		fultureServer = bf.getBean("fultureServer");
		studentServer = bf.getBean("studentServer");
	}
}
